package com.imooc.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.catalina.mbeans.UserMBean;
import org.n3r.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.mapper.CommentsMapper;
import com.imooc.mapper.CommentsMapperCustom;
import com.imooc.mapper.SearchRecordsMapper;
import com.imooc.mapper.UsersLikeVideosMapper;
import com.imooc.mapper.UsersMapper;
import com.imooc.mapper.VideosMapper;
import com.imooc.mapper.VideosMapperCustom;
import com.imooc.pojo.Comments;
import com.imooc.pojo.SearchRecords;
import com.imooc.pojo.UsersLikeVideos;
import com.imooc.pojo.Videos;
import com.imooc.pojo.vo.CommentsVO;
import com.imooc.pojo.vo.VideosVO;
import com.imooc.service.VideoService;
import com.imooc.utils.PagedResult;
import com.imooc.utils.TimeAgoUtils;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class VideoServiceImpl implements VideoService{

	@Autowired
	private VideosMapper videosMapper;
	
	@Autowired
	private UsersMapper usersMapper;
	
	@Autowired
	private CommentsMapper commentsMapper;
	
	@Autowired
	private CommentsMapperCustom commentsMapperCustom;


	@Autowired
	private VideosMapperCustom videosMapperCustom;
	
	@Autowired
	private SearchRecordsMapper searchRecordsMapper;
	
	@Autowired
	private UsersLikeVideosMapper usersLikeVideosMapper;

	@Autowired
	private Sid sid;
	
	

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public String save(Videos video) {
		
		String id=sid.nextShort();
		video.setId(id);
		videosMapper.insertSelective(video);
		return id;
	}


	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void updateVideo(String videoId, String coverPath) {
		Videos video=new Videos();
		video.setId(videoId);
		video.setCoverPath(coverPath);
		videosMapper.updateByPrimaryKeySelective(video);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public PagedResult getAllVideos(Videos video,Integer isSaveRecord,Integer page, Integer pageSize) {
		
		//保存热搜词
		String desc=video.getVideoDesc();
		String userId=video.getUserId();
		if(isSaveRecord!=null&&isSaveRecord==1) {
			SearchRecords record=new SearchRecords();
			String recrdId=sid.nextShort();
			record.setId(recrdId);
			record.setContent(desc);
			searchRecordsMapper.insert(record);
		}
		
		
		 PageHelper.startPage(page,pageSize);
		
		 List<VideosVO> list=videosMapperCustom.queryAllVideos(desc,userId);
		 
		 PageInfo<VideosVO> pageList=new PageInfo<>(list);
		 
		 PagedResult pagedResult=new PagedResult();
		 pagedResult.setPage(page);
		 pagedResult.setTotal(pageList.getPages());
		 pagedResult.setRows(list);
		 pagedResult.setRecords(pageList.getTotal());
		 
		 
		 return pagedResult;
	}

	@Transactional(propagation=Propagation.SUPPORTS)
	@Override
	public List<String> getHotWords() {
		
		return searchRecordsMapper.getHotwords();
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void userLikeVideo(String userId, String videoId, String videoCreateId) {
		//1、保存用户和视频的喜欢和点赞关联关系表
		String likeId=sid.nextShort();
		UsersLikeVideos ulv=new UsersLikeVideos();
		ulv.setId(likeId);
		ulv.setUserId(userId);
		ulv.setVideoId(videoId);
		usersLikeVideosMapper.insert(ulv);
		
		//2、视频喜欢数量增加
		videosMapperCustom.addVideoLikeCount(videoId);
		
		
	    //3、用户受喜欢数量的累加
		usersMapper.addReceiveLikeCount(videoCreateId);
	}

	@Transactional(propagation=Propagation.REQUIRED)
	@Override
	public void userUnLikeVideo(String userId, String videoId, String videoCreateId) {
		//1、删除用户和视频的喜欢点赞关联关系表
		
		Example example=new Example(UsersLikeVideos.class);
		
		Criteria criteria = example.createCriteria();
		
		criteria.andEqualTo("userId",userId);
		criteria.andEqualTo("videoId",videoId);
		
		usersLikeVideosMapper.deleteByExample(example);
		
		//2、视频喜欢数量累减
		videosMapperCustom.reduceVideoLikeCount(videoId);
		
		//3、用户受喜欢数量的累减
		
		usersMapper.reduceReceiveLikeCount(videoCreateId);
		
		
	}


	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public PagedResult queryMyLikeVideos(String userId, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<VideosVO> list = videosMapperCustom.queryMyLikeVideos(userId);
				
		PageInfo<VideosVO> pageList = new PageInfo<>(list);
		
		PagedResult pagedResult = new PagedResult();
		pagedResult.setTotal(pageList.getPages());
		pagedResult.setRows(list);
		pagedResult.setPage(page);
		pagedResult.setRecords(pageList.getTotal());
		
		return pagedResult;
	}


	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public PagedResult queryMyFollowVideos(String userId, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);
		List<VideosVO> list = videosMapperCustom.queryMyFollowVideos(userId);
				
		PageInfo<VideosVO> pageList = new PageInfo<>(list);
		
		PagedResult pagedResult = new PagedResult();
		pagedResult.setTotal(pageList.getPages());
		pagedResult.setRows(list);
		pagedResult.setPage(page);
		pagedResult.setRecords(pageList.getTotal());
		
		return pagedResult;
	}


	@Transactional(propagation = Propagation.REQUIRED)
	@Override
	public void saveComment(Comments comment) {
		String id=sid.nextShort();
		comment.setId(id);
		comment.setCreateTime(new Date());
		commentsMapper.insert(comment);
		
	}

	@Transactional(propagation = Propagation.SUPPORTS)
	@Override
	public PagedResult getAllComments(String videoId, Integer page, Integer pageSize) {

		PageHelper.startPage(page,pageSize);
		
		List<CommentsVO> list = commentsMapperCustom.queryComments(videoId);
		
		for(CommentsVO c : list) {
			String timeAgo=TimeAgoUtils.format(c.getCreateTime());
			c.setTimeAgoStr(timeAgo);
		}
		
		PageInfo<CommentsVO> pageList=new PageInfo<>(list);
		
		PagedResult grid=new PagedResult();
		grid.setTotal(pageList.getPages());
		grid.setRows(list);
		grid.setPage(page);
		grid.setRecords(pageList.getTotal());
		
		return grid;
	}
	
	
	
}





